package com.danco.training.zaneuskaya.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.danco.training.zaneuskaya.domain.Forum;
import com.danco.training.zaneuskaya.domain.Note;

import com.danco.training.zaneuskaya.repository.ForumDao;
import com.danco.training.zaneuskaya.repository.NoteDao;
import com.danco.training.zaneuskaya.service.NoteService;

/**
 * @author Zaneuskaya Aliaksandra
 *
 */
@Service("noteService")
@Transactional
public class NoteServiceImpl implements NoteService {
	
	private static final String NOTE_NOT_FOUND = "Note not found";

	

	private static final Logger logger = Logger
			.getLogger(NoteServiceImpl.class);

	@Autowired
	private NoteDao noteDao;
	
	@Autowired
	private ForumDao forumDao;
	
	

	public ForumDao getForumDao() {
		return forumDao;
	}

	public void setForumDao(ForumDao forumDao) {
		this.forumDao = forumDao;
	}

	public NoteDao getNoteDao() {
		return noteDao;
	}

	public void setNoteDao(NoteDao noteDao) {
		this.noteDao = noteDao;
	}

	@Override
	public void createEntity(Note t){
		try{
			
		noteDao.create(t);
		
			
		}
		catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		} 

	}

	@Override
	public void deleteEntityById(Integer id) {
		try{
		Note note = noteDao.read(id);
		noteDao.delete(note);
		}
		catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}
	}

	@Override
	public void updateEntity(Note t) {
		try{
			Note note = noteDao.read(t.getNoteId());
			note.setNoteText(t.getNoteText());
			note.setNoteTime(t.getNoteTime());
			noteDao.update(note);
		}
		catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}
	}

	@Override
	public Note getEntityById(Integer id) {
		Note note = null;
		try{
			note = noteDao.read(id);
			if(note == null){
				throw new NullPointerException(NOTE_NOT_FOUND);
			}
		}
		catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}
		return note;
	}

	@Override
	public List<Note> getEntityList() {
		List<Note> notes = null;
		try{
			notes =  noteDao.readAll();
		}
		catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}
		return notes;
	}

	@Override
	public List<Note> findForumNoteList(Forum forum) {
		List<Note> notes = null;
		try{
			forum = forumDao.read(forum.getForumId());
			
			notes =  noteDao.findNotesByForum(forum);
		}
		catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}
		return notes;
	}

}
